local blaze = require "blaze"
local app = require "app"
local slotlib = require "club.threecard.slotlib"
local game = app.game.running

local GameObject = require "entities.interfaces.GameObject"
local Client = require "entities.interfaces.Client"
local Player = require "entities.interfaces.Player"

local M = game.kbe.entity("SlotPlayer", GameObject, Client, Player)

function M:__init__()
    GameObject.__init__(self)
    Client.__init__(self)
    Player.__init__(self)
end

function M:onEnterSpace()
    game.store.room.player_enter(self)
end

function M:onLeaveSpace()
    slotlib.seat.reset()
    game.store.room.clear()
end

function M:onEnterWorld()
    if self.id ~= blaze.kbe.player_id() then
        -- 自己进入使用onEnterSpace事件
        game.store.room.player_enter(self)
    end
end

function M:onLeaveWorld()
    game.store.room.player_leave(self)
end

function M:onDestroy()
    if self.id == blaze.kbe.player_id() then
        slotlib.seat.reset()
        game.store.room.clear()
    end
end

--[[ =============================
             服务端接口
================================== ]]--

-- 请求旋转
-- amount UINT32
function M:spin(amount)
    self.log.debug("spin:" .. tostring(amount))
    self:cellCall("spin", amount)
end

--[[ =============================
             服务端回调
================================== ]]--
function M:set_free_times(old)
    self.log.debug(string.format("set_free_times old:%s current:%s", old, self.free_times))
    game.store.machine.on_free_times(self.free_times)
end

function M:on_cell_game_init()
end

-- 旋转结果
-- result_info SlotSpinResultInfo
function M:on_spin_result(result_info)
    self.log.debug("on_spin_result " .. blaze.dump(result_info))
    game.store.machine.on_spin_result(result_info)
end

function M:on_hit(payout)
    game.store.gameplay.player_hit({
        player = self,
        payout = payout
    })
end

return M